/* eslint-disable no-mixed-operators,function-paren-newline,no-eval */
/**
 * Created by mar105 on 2017-11-16.
 */
import React from 'react';
import moment from 'moment';
import { Form, Col } from 'antd';
import ShowType from '../../routes/common/showType';
import * as commonUtils from '../../utils/utils';
/* 根据控件名 通过配置 得到是否显示与是否有权限 */
// type control, field 控件、字段
// name 名称
// config 单表配置数据
// gdsformconst 未配置值时取常量
// gdsjurisdiction 看是否有权限功能
// sDefault 默认中文 可不传.
export function getConfig(type, name, config, gdsformconst, gdsjurisdiction, sDefault) {
  let returnConfig = {};
  returnConfig.bVisible = true;
  if (config !== undefined) {
    let nameConfig;
    if (type === 'control') {
      nameConfig = config.findIndex(item => item.sControlName === name) > -1 ? config.filter(item => item.sControlName === name)[0] : returnConfig;
    } else {
      nameConfig = config.findIndex(item => item.sName === name) > -1 ? config.filter(item => item.sName === name)[0] : returnConfig;
    }
    if (Object.keys(nameConfig).length > 0) {
      returnConfig = nameConfig;
    }
  }
  if (!(returnConfig.showName !== undefined && returnConfig.showName !== '')) {
    const nameConst = gdsformconst.findIndex(item => item.sName === name) > -1 ? gdsformconst.filter(item => item.sName === name)[0] : {};
    if (Object.keys(nameConst).length > 0) {
      returnConfig.showName = nameConst.showName;
    } else {
      returnConfig.showName = sDefault;
    }
  }
  /* const nameJurisdiction = gdsjurisdiction.findIndex(
   item => item.sName === name) > -1 ? gdsjurisdiction.filter(
   item => item.sName === name)[0] : {};
   if (Object.keys(nameJurisdiction).length > 0) {
   config.bVisible = config.bVisible && nameJurisdiction.bVisible;
   } */
  return returnConfig;
}

/* 根据配置取到 快捷过滤的字段 */
// config 所有配置数据
export function getFastConfig(config) {
  const fastConfig = config.filter(item => (item.sName !== '' && item.bVisible && item.bFind && item.showName !== '') || (item.sName.toLowerCase().indexOf('pro_') > -1));
  const returnConfig = [];
  fastConfig.forEach((item) => {
    returnConfig.push({
      ...item,
      key: item.sName,
      value: item.showName,
    });
  });
  return returnConfig;
}

/* 根据表名 获取到单个表的所有配置 */
// tableName 表名  如果表名为空 直接取非表名的控制
// allConfig 所有配置数据
export function getTableConfig(tableName, allConfig) {
  return tableName === '' ?
    allConfig.findIndex(item => !item.bGrd) > -1 ? allConfig.filter(item => !item.bGrd)[0] : { sId: '' } :
    allConfig.findIndex(item => item.bGrd && item.sGrd === tableName) > -1 ? allConfig.filter(item => item.bGrd && item.sGrd === tableName)[0] : { sId: '' };
}

/* 根据表名 获取到单个窗体的所有配置 */
// tableName 表名
// allConfig 所有配置数据
export function getWindowConfig(tableName, allConfig) {
  return allConfig.findIndex(item => item.sTbName === tableName) > -1 ? allConfig.filter(item => item.sTbName === tableName)[0] : { sId: '' };
}

/* 根据配置获取显示的列 */
// config 所有配置数据
// conditionField 过滤条件字段
export function getConfigCondition(config, conditionField) {
  return config.findIndex(item => item.sName !== '' && item[conditionField] && item.showName !== '') > -1 ? config.filter(item => item.sName !== '' && item[conditionField] && item.showName !== '') : [];
}

/* 根据配置获取显示的列 - 仅用于系统参数 */
// config 所有配置数据
// conditionField 过滤条件字段
export function getSysConfigCondition(config, conditionField) {
  return config.findIndex(item => item.sControlName !== '' && item[conditionField] && item.showName !== '') > -1 ? config.filter(item => item.sControlName !== '' && item[conditionField] && item.showName !== '') : [];
}

/* 根据配置获取复制到、上查、下查等动态数据配置
 * config 所有配置数据
 * resultType 返回的类型（copyTo-复制到(没前缀是表类型,有前缀1为视图类型:1copyTo)  3copyForm-复制从 4searchUp-上查  5searchDown-下查 ）
 */
export function getConfigCopyTo(config, resultType) {
  let returnConfig = config.filter(item => item.sName === '' && item.sControlName.indexOf(resultType) > -1);
  if (returnConfig === undefined) {
    returnConfig = [];
  }
  return returnConfig;
}

/* 筛选配置中复制到、上查、下查展示项
 * config 配置数据
 * resultType 返回的类型（copyTo-复制到 3copyForm-复制从 4searchUp-上查  5searchDown-下查）
 */

export function getConfigCopyToSelect(config, resultType) {
  let returnConfig = config.filter(item => item.sName === '' && item.sControlName.indexOf(resultType) > -1 && item.sControlName.lastIndexOf('_') === item.sControlName.indexOf('_'));
  if (returnConfig === undefined) {
    returnConfig = [];
  }
  return returnConfig;
}
/* 根据配置获取显示的列 */
// tableName 表名
// config 所有配置数据
// export function getTableConfigHeader(config) {
//   const returnConfig = getConfigCondition(config, 'bVisible');
//   return returnConfig;
// }

/* 根据配置获取显示的列 */
// tableName 表名
// config 所有配置数据
export function getTreeConfigData(config) {
  const treeConfig = getConfigCondition(config, 'bFilter');
  const returnConfig = [];
  treeConfig.map((child) => {
    returnConfig.push({
      ...child,
      sControlId: child.sId,
      key: child.sId,
      isLeaf: false,
    });
    return true;
  });
  return returnConfig;
}

/* 多模块批量新增修改删除时数据处理 */
// type 数据分类查找-add/update/del
// value 需过滤数据
/* export function getBatchHandleData(type, value) {
 const data = value.filter(item => (item.type === `${type}`));
 let resultData = [];
 for (const item of data) {
 delete item.type;
 }
 return data;
 } */

/**
 * mapPropsToFields数据转化用于自动转化绑定表单数据
 * @param obj 需转化数据
 */
export function mapPropsToFieldsChangeValue(config, obj) {
  const result = {};
  Object.keys(config).map((el) => {
    const { sDefault } = config[el];
    if (config[el].sDropDownType === 'const' || config[el].sDropDownType === 'sql') {
      /* 常量下拉和动态sql下拉没有设定默认设置下拉为空 */
      if (config[el].bFirstEmpty) {
        result[config[el].sName] = Form.createFormField({ value: obj[config[el].sName] !== undefined ? obj[config[el].sName] : (sDefault !== '' ? sDefault : ' ') });
      } else {
        result[config[el].sName] = Form.createFormField({ value: obj[config[el].sName] !== undefined ? obj[config[el].sName] : (sDefault !== '' ? sDefault : null) });
      }
    } else if (config[el].sDropDownType === 'popup') {
      /* 弹出框数据处理 */
      let v = obj[config[el].sName];
      if (obj[config[el].sName] !== undefined && obj[config[el].sName].indexOf('[-]')) {
        [v] = obj[config[el].sName].split('[-]');
      }
      result[config[el].sName] = Form.createFormField({ value: obj[config[el].sName] !== undefined ? v : (sDefault !== '' ? sDefault : null) });
    } else {
      const value = Object.keys(obj).length > 0 ? obj[config[el].sName] : undefined;
      const dateFormat = 'YYYY-MM-DD HH:mm:ss'; /* 日期控件显示格式 */
      /* 主表默认值处理 */
      /* boolean值处理 */
      if (config[el].sName.substring(0, 1) === 'b') {
        result[config[el].sName] = Form.createFormField({ value: (value !== undefined ? value : Boolean(sDefault)) });
      } else if (config[el].sName.substring(0, 1) === 't') {
        /* 时间处理 */
        result[config[el].sName] = Form.createFormField({ value: (value !== undefined ? (value !== null ? moment(value, dateFormat) : null) : (sDefault !== '' ? moment(sDefault, dateFormat) : null)) });
      } else if (config[el].sName.substring(0, 1) === 'c') {
        /* 省市区地址 */
        result[config[el].sName] = Form.createFormField({ value: (value !== undefined ? value : sDefault.split(',')) });
      } else {
        result[config[el].sName] = Form.createFormField({ value: value !== undefined ? value : (sDefault !== '' ? sDefault : null) });
      }
    }
    return true;
  });
  return result;
}

/**
 * sysPropsToFields数据转化用于自动转化绑定表单数据，只针对于系统参数
 * @param obj 需转化数据
 */
export function sysPropsToFieldsChangeValue(config, obj) {
  const result = {};
  Object.keys(obj).map((el) => {
    const data = config.filter(item => item.sControlName === obj[el].sName)[0];
    const { sDefault, sControlName } = data;
    if (data.sDropDownType === 'const' || data.sDropDownType === 'sql') {
      /* 常量下拉和动态sql下拉没有设定默认设置下拉为空 */
      result[sControlName] = Form.createFormField({ value: obj[el].sValue !== undefined ? obj[el].sValue : (sDefault !== '' ? sDefault : ' ') });
    } else if (data.sDropDownType === 'popup') {
      /* 弹出框数据处理 */
      let v = obj[el].sValue;
      if (v !== undefined && v.indexOf('[-]')) {
        [v] = v.split('[-]');
      }
      result[sControlName] = Form.createFormField({ value: obj[el].sValue !== undefined ? v : (sDefault !== '' ? sDefault : null) });
    } else {
      const value = obj.length > 0 ? obj[el].sValue : undefined;
      const dateFormat = 'YYYY-MM-DD HH:mm:ss'; /* 日期控件显示格式 */
      /* 主表默认值处理 */
      /* boolean值处理 */
      if (data.sControlName.substring(0, 1) === 'b') {
        result[sControlName] = Form.createFormField({ value: (value !== undefined ? value !== '0' : Boolean(sDefault)) });
      } else if (data.sControlName.substring(0, 1) === 't') {
        /* 时间处理 */
        result[sControlName] = Form.createFormField({ value: (value !== undefined ? (value !== null ? moment(value, dateFormat) : null) : (sDefault !== '' ? moment(sDefault, dateFormat) : null)) });
      } else {
        result[sControlName] = Form.createFormField({ value: value !== undefined ? value : (sDefault !== '' ? sDefault : null) });
      }
    }
    return '';
  });
  return result;
}

/**
 * 列表数据默认值展示处理
 * @param value 需转化数据为单个字段具体值 valueConfig 单个字段配置
 *
 */
export function listDefaultValue(valueConfig, value) {
  let resultValue = '';
  const dateFormat = 'YYYY-MM-DD HH:mm:ss';
  if (valueConfig.handleType === 'add') {
    /* 返回值 */
    const { sDefault } = valueConfig;
    /* 日期控件显示格式 后期需要根据配置的格式进行单独转化,需在salver表中新增格式化字段 */
    const sName = valueConfig.sName.substring(0, 1);
    /* boolean值处理 */
    if (sName === 'b') {
      resultValue = value !== undefined && value !== '' ? value : Boolean(sDefault);
    } else if (sName === 't') {
      /* 时间处理 */
      resultValue = (value !== undefined && value !== '' ? (value !== null ? moment(value, dateFormat) : null) : moment(sDefault, dateFormat));
    } else if (sName === 'd' || sName === 'i') {
      /* double类型、int类型处理 */
      resultValue = (value !== undefined && value !== '' ? value : sDefault);
    } else {
      resultValue = value !== undefined && value !== '' ? value : sDefault;
    }
  } else {
    const sName = valueConfig.sName.substring(0, 1);
    if (sName === 't') {
      /* 时间处理 */
      resultValue = (value !== undefined && value !== '' ? (value !== null ? moment(value, dateFormat) : null) : null);
    } else {
      resultValue = value;
    }
  }
  return resultValue;
}

/**
 * 列表数据默认值新增处理
 * @param value 需转化数据为单个字段具体值 valueConfig 单个字段配置
 *
 */
export function listDefaultValueNewAdd(valueConfig, value) {
  /* 返回值 */
  let resultValue = '';
  const { sDefault } = valueConfig;
  const sName = valueConfig.sName.substring(0, 1);
  /* boolean值处理 */
  if (sName === 'b') {
    resultValue = value !== undefined && value !== '' ? value : Boolean(sDefault);
  } else if (sName === 't') {
    /* 时间处理 */
    resultValue = (value !== undefined && value !== '' ? (value !== null ? value : null) : sDefault);
  } else if (sName === 'd' || sName === 'i') {
    /* double类型、int类型处理 */
    resultValue = (value !== undefined && value !== '' ? value : sDefault);
  } else if (sName === 'c') {
    /* 省市区 */
    resultValue = (value !== undefined && value !== '' ? value : []);
  } else {
    resultValue = value !== undefined && value !== '' ? value : sDefault;
  }
  return resultValue;
}

/**
 * 数据变更用于对不同类型做转换处理
 * @param obj 需转化数据
 * 数据变更每次只会变更一个字段数据，所以value中只取第一个
 */
export function changeValue(value) {
  const objkey = Object.keys(value)[0];
  /* double类型和int类型手动清空undefined处理 */
  if ((objkey.substring(0, 1) === 'd' || objkey.substring(0, 1) === 'i') && (value[objkey] === undefined || value[objkey] === '')) {
    value[objkey] = 0;
  }
  return value;
}

/**
 * 数据变更用于对不同类型做转换处理
 * @param obj 需转化数据
 * 数据变更每次只会变更一个字段数据，所以value中只取第一个, 专门针对系统参数
 */
export function changeSystemValue(value) {
  const objkey = Object.keys(value)[0];
  /* double类型和int类型手动清空undefined处理 */
  if ((objkey.substring(0, 1) === 'd' || objkey.substring(0, 1) === 'i') && (value[objkey] === undefined || value[objkey] === '')) {
    value[objkey] = 0;
  }
  if (objkey.substring(0, 1) === 'b') {
    if (value[objkey] === true) {
      value[objkey] = '1';
    } else {
      value[objkey] = '0';
    }
  }
  return value;
}

/** 输入类型，传值 */
export function getResultType(obj, sName, decimals) {
  const { sType } = obj;
  let resultValue = {};
  if (sType === 'dProductPrice') {
    /* 数量 */
    resultValue = this.getResultByPrice(obj, sName, decimals);
  } else if (sType === 'dProductMoney') {
    /* 金额 */
    resultValue = this.getResultByMoney(obj, sName, decimals);
  } else if (sType === 'dProductNoTaxPrice') {
    /* 不含税价格 */
    resultValue = this.getResultByNoTaxPrice(obj, sName, decimals);
  } else if (sType === 'dProductNoTaxMoney') {
    /* 不含税金额 */
    resultValue = this.getResultByNoTaxMoney(obj, sName, decimals);
  } else if (sType === 'dCurrencyRate') {
    /* 汇率 */
    resultValue = this.getResultByCurrencyRate(obj, sName, decimals);
  } else if (sType === 'dLossMoney') {
    /* 报损金额 */
    resultValue = this.getResultByDLossPrice(obj, sName, decimals);
  }
  return resultValue;
}

/** 输入报损数量, 价格 */
export function getResultByDLossPrice(obj, sName, decimals) {
  const { dNetMoney } = decimals;
  let { dLossQty, dProductPrice } = obj;
  dLossQty = dLossQty === undefined || dLossQty === '' || isNaN(dLossQty) ? 0 : dLossQty;
  dProductPrice = dProductPrice === undefined || dProductPrice === '' || isNaN(dProductPrice) ? 0 : dProductPrice;
  return (dLossQty * dProductPrice).toFixed(dNetMoney); /* 报损金额 */
}

/** 输入数量, 价格 */
export function getResultByPrice(obj, sName, decimals) {
  const { dNetPrice, dNetMoney } = decimals;
  let {
    dProductQty, dProductPrice, dTaxRate, dCurrencyRate,
  } = obj;
  const { dLossQty } = obj;
  dProductQty = dProductQty === undefined || dProductQty === '' || isNaN(dProductQty) ? 0 : dProductQty;
  dProductPrice = dProductPrice === undefined || dProductPrice === '' || isNaN(dProductPrice) ? 0 : dProductPrice;
  dTaxRate = dTaxRate === undefined || dTaxRate === '' || isNaN(dTaxRate) ? 0 : dTaxRate;
  dCurrencyRate = dCurrencyRate === undefined || dCurrencyRate === '' || isNaN(dCurrencyRate) ? 1 : dCurrencyRate;
  const dProductMoney = dProductQty * dProductPrice; /* 金额 */
  const dProductNoTaxMoney = dProductMoney / (1 + ((dTaxRate * 1) / 100)); /* 不含税金额 */
  const dProductNoTaxPrice = parseFloat(dProductQty) !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价 */
  const dProductTaxMoney = dProductMoney - dProductNoTaxMoney; /* 税额 */
  const dProductForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductMoney / (dCurrencyRate * 1) : 0; /* 外币金额 */
  const dProductTaxForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductTaxMoney / (dCurrencyRate * 1) : 0; /* 外币税额 */
  const dLossMoney = dProductPrice * dLossQty; /* 报损金额 */
  const resultValue = {};
  resultValue[`d${sName}Money`] = dProductMoney.toFixed(dNetMoney);
  resultValue[`d${sName}NoTaxMoney`] = dProductNoTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}NoTaxPrice`] = dProductNoTaxPrice.toFixed(dNetPrice);
  resultValue[`d${sName}TaxMoney`] = dProductTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}ForeignMoney`] = dProductForeignMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxForeignMoney`] = dProductTaxForeignMoney.toFixed(dNetMoney);
  if (dLossQty !== undefined && !isNaN(dLossQty)) {
    resultValue.dLossMoney = dLossMoney.toFixed(dNetMoney);
  }
  return resultValue;
}

/** 输入金额 */
export function getResultByMoney(obj, sName, decimals) {
  const { dNetPrice, dNetMoney } = decimals;
  let {
    dProductQty, dProductMoney, dTaxRate, dCurrencyRate,
  } = obj;
  dProductQty = dProductQty === undefined || dProductQty === '' || isNaN(dProductQty) ? 0 : dProductQty;
  dProductMoney = dProductMoney === undefined || dProductMoney === '' || isNaN(dProductMoney) ? 0 : dProductMoney;
  dTaxRate = dTaxRate === undefined || dTaxRate === '' || isNaN(dTaxRate) ? 0 : dTaxRate;
  dCurrencyRate = dCurrencyRate === undefined || dCurrencyRate === '' || isNaN(dCurrencyRate) ? 1 : dCurrencyRate;

  const dProductPrice = parseFloat(dProductQty) !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductNoTaxMoney = dProductMoney / (1 + ((dTaxRate * 1) / 100)); /* 不含税金额 */
  const dProductNoTaxPrice = parseFloat(dProductQty) !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价 */
  const dProductTaxMoney = dProductMoney - dProductNoTaxMoney; /* 税额 */
  const dProductForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductMoney / (dCurrencyRate * 1) : 0; /* 外币金额 */
  const dProductTaxForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductTaxMoney / (dCurrencyRate * 1) : 0; /* 外币税额 */
  const resultValue = {};
  resultValue[`d${sName}Price`] = dProductPrice.toFixed(dNetPrice);
  resultValue[`d${sName}NoTaxMoney`] = dProductNoTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}NoTaxPrice`] = dProductNoTaxPrice.toFixed(dNetPrice);
  resultValue[`d${sName}TaxMoney`] = dProductTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}ForeignMoney`] = dProductForeignMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxForeignMoney`] = dProductTaxForeignMoney.toFixed(dNetMoney);
  return resultValue;
}

/** 输入不含税价格 */
export function getResultByNoTaxPrice(obj, sName, decimals) {
  const { dNetPrice, dNetMoney } = decimals;
  let {
    dProductQty, dProductNoTaxPrice, dTaxRate, dCurrencyRate,
  } = obj;
  dProductQty = dProductQty === undefined || dProductQty === '' || isNaN(dProductQty) ? 0 : dProductQty;
  dProductNoTaxPrice = dProductNoTaxPrice === undefined || dProductNoTaxPrice === '' || isNaN(dProductNoTaxPrice) ? 0 : dProductNoTaxPrice;
  dTaxRate = dTaxRate === undefined || dTaxRate === '' || isNaN(dTaxRate) ? 0 : dTaxRate;
  dCurrencyRate = dCurrencyRate === undefined || dCurrencyRate === '' || isNaN(dCurrencyRate) ? 1 : dCurrencyRate;

  const dProductNoTaxMoney = dProductQty * dProductNoTaxPrice; /* 不含税金额 */
  const dProductTaxMoney = dProductNoTaxMoney * ((dTaxRate * 1) / 100); /* 税额 */
  const dProductMoney = dProductNoTaxMoney + dProductTaxMoney; /* 金额 */
  const dProductPrice = parseFloat(dProductQty) !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductMoney / (dCurrencyRate * 1) : 0; /* 外币金额 */
  const dProductTaxForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductTaxMoney / (dCurrencyRate * 1) : 0; /* 外币税额 */
  const resultValue = {};
  resultValue[`d${sName}NoTaxMoney`] = dProductNoTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxMoney`] = dProductTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}Money`] = dProductMoney.toFixed(dNetMoney);
  resultValue[`d${sName}Price`] = dProductPrice.toFixed(dNetPrice);
  resultValue[`d${sName}ForeignMoney`] = dProductForeignMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxForeignMoney`] = dProductTaxForeignMoney.toFixed(dNetMoney);
  return resultValue;
}

/** 输入不含税金额 */
export function getResultByNoTaxMoney(obj, sName, decimals) {
  const { dNetPrice, dNetMoney } = decimals;
  let {
    dProductQty, dProductNoTaxMoney, dTaxRate, dCurrencyRate,
  } = obj;
  dProductQty = dProductQty === undefined || dProductQty === '' || isNaN(dProductQty) ? 0 : dProductQty;
  dProductNoTaxMoney = dProductNoTaxMoney === undefined || dProductNoTaxMoney === '' || isNaN(dProductNoTaxMoney) ? 0 : dProductNoTaxMoney;
  dTaxRate = dTaxRate === undefined || dTaxRate === '' || isNaN(dTaxRate) ? 0 : dTaxRate;
  dCurrencyRate = dCurrencyRate === undefined || dCurrencyRate === '' || isNaN(dCurrencyRate) ? 1 : dCurrencyRate;

  const dProductNoTaxPrice = parseFloat(dProductQty) !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价格 */
  const dProductTaxMoney = dProductNoTaxMoney * ((dTaxRate * 1) / 100); /* 税额 */
  const dProductMoney = dProductNoTaxMoney + dProductTaxMoney; /* 金额 */
  const dProductPrice = parseFloat(dProductQty) !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductMoney / (dCurrencyRate * 1) : 0; /* 外币金额 */
  const dProductTaxForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductTaxMoney / (dCurrencyRate * 1) : 0; /* 外币税额 */
  const resultValue = {};
  resultValue[`d${sName}NoTaxPrice`] = dProductNoTaxPrice.toFixed(dNetPrice);
  resultValue[`d${sName}TaxMoney`] = dProductTaxMoney.toFixed(dNetMoney);
  resultValue[`d${sName}Money`] = dProductMoney.toFixed(dNetMoney);
  resultValue[`d${sName}Price`] = dProductPrice.toFixed(dNetPrice);
  resultValue[`d${sName}ForeignMoney`] = dProductForeignMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxForeignMoney`] = dProductTaxForeignMoney.toFixed(dNetMoney);
  return resultValue;
}

/** 汇率 */
export function getResultByCurrencyRate(obj, sName, decimals) {
  const { dNetMoney } = decimals;
  let { dProductMoney, dProductTaxMoney, dCurrencyRate } = obj;
  dProductMoney = dProductMoney === undefined || dProductMoney === '' || isNaN(dProductMoney) ? 0 : dProductMoney;
  dProductTaxMoney = dProductTaxMoney === undefined || dProductTaxMoney === '' || isNaN(dProductTaxMoney) ? 0 : dProductTaxMoney;
  dCurrencyRate = dCurrencyRate === undefined || dCurrencyRate === '' || isNaN(dCurrencyRate) ? 1 : dCurrencyRate;

  const dProductForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductMoney / (dCurrencyRate * 1) : 0; /* 外币金额 */
  const dProductTaxForeignMoney = (dCurrencyRate * 1) !== 0 ? dProductTaxMoney / (dCurrencyRate * 1) : 0; /* 外币税额 */
  const resultValue = {};
  resultValue[`d${sName}ForeignMoney`] = dProductForeignMoney.toFixed(dNetMoney);
  resultValue[`d${sName}TaxForeignMoney`] = dProductTaxForeignMoney.toFixed(dNetMoney);
  return resultValue;
}


/** 计算换算单位--正算 */
export function getResultByStyle(obj) {
  let {
    sType, sMaterialsStyle, dAuxiliaryQty, sMaterialsGramWeight, dCoefficient,
  } = obj;
  sType = sType === undefined ? '' : sType;
  sMaterialsStyle = sMaterialsStyle === undefined ? '' : sMaterialsStyle;
  dAuxiliaryQty = dAuxiliaryQty === undefined || dAuxiliaryQty === '' || isNaN(dAuxiliaryQty) ? 0 : dAuxiliaryQty;
  let dMaterialsQty = 0;
  sMaterialsGramWeight = sMaterialsGramWeight === undefined || sMaterialsGramWeight === '' || isNaN(sMaterialsGramWeight) ? 0 : sMaterialsGramWeight;
  dCoefficient = dCoefficient === undefined || dCoefficient === '' || isNaN(dCoefficient) ? 1 : dCoefficient;

  const sMStyleO = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[0] : '';
  const sMStyleT = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[1] !== '' ? sMaterialsStyle.split('*')[1] : 1 : '';
  if (sType === 't' || sType === 'kg') {
    /* 按重量 */
    const multiple = sType === 't' ? 1000000000000 : 1000000000;
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT) * sMaterialsGramWeight) / multiple).toFixed(6);
  } else if (sType === 'm2') {
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT)) / 1000000).toFixed(6);
  } else if (sType === 'm2Reel') {
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT)) / 1000).toFixed(6);
  } else if (sType === 'tNo' || sType === 'kgNo') {
    const multiple = sType === 'tNo' ? 1000000000000 : 1000000000;
    dMaterialsQty = ((dAuxiliaryQty * sMStyleO * sMaterialsGramWeight) / multiple).toFixed(6);
  } else if (sType === 'm2ReelNo') {
    dMaterialsQty = ((dAuxiliaryQty * sMStyleO) / 1000).toFixed(6);
  } else {
    dMaterialsQty = parseFloat(dCoefficient) === 0 ? dAuxiliaryQty.toFixed(6) : (dAuxiliaryQty * dCoefficient).toFixed(6);
  }
  return dMaterialsQty;
}

/** 计算库存数量--反算 */
export function getResultByMQty(obj) {
  let {
    sType, sMaterialsStyle, dAuxiliaryQty, dMaterialsQty, sMaterialsGramWeight, dCoefficient,
  } = obj;
  sType = sType === undefined ? '' : sType;
  sMaterialsStyle = sMaterialsStyle === undefined ? '' : sMaterialsStyle;
  dAuxiliaryQty = dAuxiliaryQty === undefined || dAuxiliaryQty === '' || isNaN(dAuxiliaryQty) ? 0 : dAuxiliaryQty;
  dMaterialsQty = dMaterialsQty === undefined || dMaterialsQty === '' || isNaN(dMaterialsQty) ? 0 : dMaterialsQty;
  sMaterialsGramWeight = sMaterialsGramWeight === undefined || sMaterialsGramWeight === '' || isNaN(sMaterialsGramWeight) ? 0 : sMaterialsGramWeight;
  dCoefficient = dCoefficient === undefined || dCoefficient === '' || isNaN(dCoefficient) ? 1 : dCoefficient;

  const sMStyleO = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[0] : '';
  const sMStyleT = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[1] !== '' ? sMaterialsStyle.split('*')[1] : 1 : '';
  if (sType === 't' || sType === 'kg') {
    /* 按重量 */
    const multiple = sType === 't' ? 1000000000000 : 1000000000;
    dAuxiliaryQty = ((dMaterialsQty * multiple) / sMaterialsGramWeight / (sMStyleO * sMStyleT)).toFixed(0);
  } else if (sType === 'm2') {
    dAuxiliaryQty = ((dMaterialsQty * 1000000) / (sMStyleO * sMStyleT)).toFixed(2);
  } else if (sType === 'm2Reel') {
    dAuxiliaryQty = ((dMaterialsQty * 1000) / (sMStyleO * sMStyleT)).toFixed(2);
  } else if (sType === 'tNo' || sType === 'kgNo') {
    const multiple = sType === 'tNo' ? 1000000000000 : 1000000000;
    dAuxiliaryQty = ((dMaterialsQty * multiple) / sMaterialsGramWeight / sMStyleO).toFixed(0);
  } else if (sType === 'm2ReelNo') {
    dAuxiliaryQty = (dMaterialsQty * 1000) / sMStyleO;
  } else {
    dAuxiliaryQty = parseFloat(dCoefficient) === 0 ? dMaterialsQty.toFixed(0) : (dMaterialsQty / dCoefficient).toFixed(0);
  }
  return dAuxiliaryQty;
}
/**
 * 列表通用复制到数据过滤
 * vConfig 列表窗体配置
 * dataList 列表数据
 * sSlaveIds 勾选数据从表ID
 * */
export function getListCopyData(vConfig, dataList, sSlaveIds, masterConfig) {
  /* 复制过去的主表数据 */
  const masterData = {};
  /* 复制过去的从表数据 */
  const masterSlaveData = [];
  /* 复制过去的从表2数据 */
  const masterSlaveData2 = [];
  /* 复制过去的从表3数据 */
  const masterSlaveData3 = [];
  /* 复制过去的从表4数据 */
  const masterSlaveData4 = [];
  /* 不符合筛选数据 */
  let errorName = '';
  for (const item of vConfig) {
    if (item.sActiveId !== undefined && item.sActiveId !== '') {
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      /* 过滤是否存在重复数据 */
      if (item.sNotRepeatColumn) {
        /* 目前sNotRepeatColumn暂只处理单个字段不可重复,如需多个字段过滤则需要再次添加处理 */
        const repeactColumns = [];
        for (const rc of dataList) {
          /* 只复制被勾选的数据 */
          if (sSlaveIds.indexOf(rc.sSlaveId) > -1) {
            if (repeactColumns.length > 0 && repeactColumns.indexOf(rc[`${item.sNotRepeatColumn}`]) <= -1) {
              const showName = masterConfig.gdsconfigformslave.filter(v => (v.sName === item.sNotRepeatColumn))[0];
              errorName = showName !== undefined ? showName.showName : item.sNotRepeatColumn;
              break;
            } else {
              repeactColumns.push(rc[`${item.sNotRepeatColumn}`]);
            }
          }
        }
      }
      if (errorName === '') {
        let data = {};
        for (const dataItem of dataList) {
          /* 只复制被勾选的数据 */
          if (sSlaveIds.indexOf(dataItem.sSlaveId) > -1) {
            data = dataItem;
          }
        }
        for (const child of Object.keys(assignFields)) {
          const fieldKey = assignFields[`${child}`];
          /* 处理赋值字段中[-]拼接 */
          if (fieldKey.indexOf('[-]') > 1) {
            const key = fieldKey.split('[-]')[0];
            const key1 = fieldKey.split('[-]')[1];
            masterData[`${child}`] = `${data[`${key}`]}[-]${data[`${key1}`]}`;
          } else {
            masterData[`${child}`] = data[`${fieldKey}`];
          }
        }
      }
    } else if (item.sControlName.indexOf('_2')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of dataList) {
        /* 只复制被勾选的数据 */
        if (sSlaveIds.indexOf(v.sSlaveId) > -1) {
          /* 过滤是否存在重复数据 */
          if (item.sNotRepeatColumn) {
            /* 目前sNotRepeatColumn暂只处理单个字段不可重复,如需多个字段过滤则需要再次添加处理 */
            const repeactColumns = [];
            for (const rc of dataList) {
              if (repeactColumns.indexOf(rc[`${item.sNotRepeatColumn}`]) > -1) {
                const showName = masterConfig.gdsconfigformslave.filter(v2 => (v2.sName === item.sNotRepeatColumn))[0];
                errorName = showName !== undefined ? showName.showName : item.sNotRepeatColumn;
                break;
              } else {
                repeactColumns.push(rc[`${item.sNotRepeatColumn}`]);
              }
            }
          }
          if (errorName === '') {
            const slaveValue = {};
            for (const child of Object.keys(assignFields)) {
              const fieldKey = assignFields[`${child}`];
              /* 处理赋值字段中[-]拼接 */
              if (fieldKey.indexOf('[-]') > -1) {
                const key = fieldKey.split('[-]')[0];
                const key1 = fieldKey.split('[-]')[1];
                slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
              } else {
                slaveValue[`${child}`] = v[`${fieldKey}`];
              }
            }
            slaveValue.sId = v.sSlaveId;
            masterSlaveData.push(slaveValue);
          }
        }
      }
    } else if (item.sControlName.indexOf('_3')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of dataList) {
        /* 只复制被勾选的数据 */
        if (sSlaveIds.indexOf(v.sSlaveId) > -1) {
          /* 过滤是否存在重复数据 */
          if (item.sNotRepeatColumn) {
            /* 目前sNotRepeatColumn暂只处理单个字段不可重复,如需多个字段过滤则需要再次添加处理 */
            const repeactColumns = [];
            for (const rc of dataList) {
              if (repeactColumns.indexOf(rc[`${item.sNotRepeatColumn}`]) > -1) {
                const showName = masterConfig.gdsconfigformslave.filter(v3 => (v3.sName === item.sNotRepeatColumn))[0];
                errorName = showName !== undefined ? showName.showName : item.sNotRepeatColumn;
                errorName = item.sNotRepeatColumn;
                break;
              } else {
                repeactColumns.push(rc[`${item.sNotRepeatColumn}`]);
              }
            }
          }
          if (errorName === '') {
            const slaveValue = {};
            for (const child of Object.keys(assignFields)) {
              const fieldKey = assignFields[`${child}`];
              /* 处理赋值字段中[-]拼接 */
              if (fieldKey.indexOf('[-]') > -1) {
                const key = fieldKey.split('[-]')[0];
                const key1 = fieldKey.split('[-]')[1];
                slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
              } else {
                slaveValue[`${child}`] = v[`${fieldKey}`];
              }
            }
            slaveValue.sId = v.sSlaveId;
            masterSlaveData2.push(slaveValue);
          }
        }
      }
    } else if (item.sControlName.indexOf('_4')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of dataList) {
        /* 只复制被勾选的数据 */
        if (sSlaveIds.indexOf(v.sSlaveId) > -1) {
          /* 过滤是否存在重复数据 */
          if (item.sNotRepeatColumn) {
            /* 目前sNotRepeatColumn暂只处理单个字段不可重复,如需多个字段过滤则需要再次添加处理 */
            const repeactColumns = [];
            for (const rc of dataList) {
              if (repeactColumns.indexOf(rc[`${item.sNotRepeatColumn}`]) > -1) {
                const showName = masterConfig.gdsconfigformslave.filter(v4 => (v4.sName === item.sNotRepeatColumn))[0];
                errorName = showName !== undefined ? showName.showName : item.sNotRepeatColumn;
                break;
              } else {
                repeactColumns.push(rc[`${item.sNotRepeatColumn}`]);
              }
            }
          }
          if (errorName === '') {
            const slaveValue = {};
            for (const child of Object.keys(assignFields)) {
              const fieldKey = assignFields[`${child}`];
              /* 处理赋值字段中[-]拼接 */
              if (fieldKey.indexOf('[-]') > -1) {
                const key = fieldKey.split('[-]')[0];
                const key1 = fieldKey.split('[-]')[1];
                slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
              } else {
                slaveValue[`${child}`] = v[`${fieldKey}`];
              }
            }
            slaveValue.sId = v.sSlaveId;
            masterSlaveData3.push(slaveValue);
          }
        }
      }
    } else if (item.sControlName.indexOf('_5')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of dataList) {
        /* 只复制被勾选的数据 */
        if (sSlaveIds.indexOf(v.sSlaveId) > -1) {
          /* 过滤是否存在重复数据 */
          if (item.sNotRepeatColumn) {
            /* 目前sNotRepeatColumn暂只处理单个字段不可重复,如需多个字段过滤则需要再次添加处理 */
            const repeactColumns = [];
            for (const rc of dataList) {
              if (repeactColumns.indexOf(rc[`${item.sNotRepeatColumn}`]) > -1) {
                const showName = masterConfig.gdsconfigformslave.filter(v5 => (v5.sName === item.sNotRepeatColumn))[0];
                errorName = showName !== undefined ? showName.showName : item.sNotRepeatColumn;
                break;
              } else {
                repeactColumns.push(rc[`${item.sNotRepeatColumn}`]);
              }
            }
          }
          if (errorName === '') {
            const slaveValue = {};
            for (const child of Object.keys(assignFields)) {
              const fieldKey = assignFields[`${child}`];
              /* 处理赋值字段中[-]拼接 */
              if (fieldKey.indexOf('[-]') > -1) {
                const key = fieldKey.split('[-]')[0];
                const key1 = fieldKey.split('[-]')[1];
                slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
              } else {
                slaveValue[`${child}`] = v[`${fieldKey}`];
              }
            }
            slaveValue.sId = v.sSlaveId;
            masterSlaveData4.push(slaveValue);
          }
        }
      }
    }
  }
  return {
    masterData, masterSlaveData, masterSlaveData2, masterSlaveData3, masterSlaveData4, errorName,
  };
}

/**
 * 单据通用复制到数据过滤(一张从表)
 * vConfig 列表窗体配置
 * billMasterData 主表数据
 * billMasterSlaveData 从表数据
 * 复制字段中用#号括起来的字段为从主表获取数据
 * */
export function getBillCopyData(vConfig, billMasterData, billMasterSlaveData, billMasterSlaveData2, billMasterSlaveData3, billMasterSlaveData4) {
  /* 复制过去的主表数据 */
  const masterData = {};
  /* 复制过去的从表数据 */
  const masterSlaveData = [];
  /* 复制过去的从表2数据 */
  const masterSlaveData2 = [];
  /* 复制过去的从表3数据 */
  const masterSlaveData3 = [];
  /* 复制过去的从表4数据 */
  const masterSlaveData4 = [];
  for (const item of vConfig) {
    if (item.sActiveId !== undefined && item.sActiveId !== '') {
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      /* 主表数据 */
      for (const child of Object.keys(assignFields)) {
        const fieldKey = assignFields[`${child}`];
        /* 处理赋值字段中[-]拼接 */
        if (fieldKey !== undefined && fieldKey.indexOf('[-]') > 1) {
          const key = fieldKey.split('[-]')[0];
          const key1 = fieldKey.split('[-]')[1];
          masterData[`${child}`] = `${billMasterData[`${key}`]}[-]${billMasterData[`${key1}`]}`;
        } else {
          masterData[`${child}`] = billMasterData[`${fieldKey}`];
        }
      }
    } else if (item.sControlName !== undefined && item.sControlName.indexOf('_2')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of billMasterSlaveData) {
        const slaveValue = {};
        for (const child of Object.keys(assignFields)) {
          let fieldKey = assignFields[`${child}`];
          /* #括起来为从主表获取 */
          if (fieldKey.startsWith('#') && fieldKey.endsWith('#')) {
            fieldKey = fieldKey.substr(1, fieldKey.length - 2);
            /* 存在#key[-]value#形式 */
            if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
              const fieldSplit = fieldKey.split('[-]');
              slaveValue[`${child}`] = `${billMasterData[`${fieldSplit[0]}`]}[-]${billMasterData[`${fieldSplit[0]}`]}`;
            } else {
              slaveValue[`${child}`] = billMasterData[`${fieldKey}`];
            }
          } else if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
            /* 处理赋值字段中[-]拼接 */
            const key = fieldKey.split('[-]')[0];
            const key1 = fieldKey.split('[-]')[1];
            slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
          } else {
            slaveValue[`${child}`] = v[`${fieldKey}`];
          }
        }
        slaveValue.sId = v.sId;
        masterSlaveData.push(slaveValue);
      }
    } else if (item.sControlName !== undefined && item.sControlName.indexOf('_3')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of billMasterSlaveData2) {
        const slaveValue = {};
        for (const child of Object.keys(assignFields)) {
          let fieldKey = assignFields[`${child}`];
          /* #括起来为从主表获取 */
          if (fieldKey.startsWith('#') && fieldKey.endsWith('#')) {
            fieldKey = fieldKey.substr(1, fieldKey.length - 2);
            /* 存在#key[-]value#形式 */
            if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
              const fieldSplit = fieldKey.split('[-]');
              slaveValue[`${child}`] = `${billMasterData[`${fieldSplit[0]}`]}[-]${billMasterData[`${fieldSplit[0]}`]}`;
            } else {
              slaveValue[`${child}`] = billMasterData[`${fieldKey}`];
            }
          } else if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
            /* 处理赋值字段中[-]拼接 */
            const key = fieldKey.split('[-]')[0];
            const key1 = fieldKey.split('[-]')[1];
            slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
          } else {
            slaveValue[`${child}`] = v[`${fieldKey}`];
          }
        }
        slaveValue.sId = v.sId;
        masterSlaveData2.push(slaveValue);
      }
    } else if (item.sControlName !== undefined && item.sControlName.indexOf('_4')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of billMasterSlaveData3) {
        const slaveValue = {};
        for (const child of Object.keys(assignFields)) {
          let fieldKey = assignFields[`${child}`];
          /* #括起来为从主表获取 */
          if (fieldKey.startsWith('#') && fieldKey.endsWith('#')) {
            fieldKey = fieldKey.substr(1, fieldKey.length - 2);
            /* 存在#key[-]value#形式 */
            if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
              const fieldSplit = fieldKey.split('[-]');
              slaveValue[`${child}`] = `${billMasterData[`${fieldSplit[0]}`]}[-]${billMasterData[`${fieldSplit[0]}`]}`;
            } else {
              slaveValue[`${child}`] = billMasterData[`${fieldKey}`];
            }
          } else if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
            /* 处理赋值字段中[-]拼接 */
            const key = fieldKey.split('[-]')[0];
            const key1 = fieldKey.split('[-]')[1];
            slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
          } else {
            slaveValue[`${child}`] = v[`${fieldKey}`];
          }
        }
        slaveValue.sId = v.sId;
        masterSlaveData3.push(slaveValue);
      }
    } else if (item.sControlName !== undefined && item.sControlName.indexOf('_5')) {
      /* 第一个从表数据 */
      /* 赋值字段对象 */
      const assignFields = {};
      if (item.sAssignField !== undefined && item.sAssignField !== '') {
        /* 第一次分割 */
        const child = item.sAssignField.split(',');
        for (const v of child) {
          /* 第二次分割 */
          const splitV = v.split(':');
          [, assignFields[`${splitV[0]}`]] = splitV;
        }
      }
      for (const v of billMasterSlaveData4) {
        const slaveValue = {};
        for (const child of Object.keys(assignFields)) {
          let fieldKey = assignFields[`${child}`];
          /* #括起来为从主表获取 */
          if (fieldKey.startsWith('#') && fieldKey.endsWith('#')) {
            fieldKey = fieldKey.substr(1, fieldKey.length - 2);
            /* 存在#key[-]value#形式 */
            if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
              const fieldSplit = fieldKey.split('[-]');
              slaveValue[`${child}`] = `${billMasterData[`${fieldSplit[0]}`]}[-]${billMasterData[`${fieldSplit[0]}`]}`;
            } else {
              slaveValue[`${child}`] = billMasterData[`${fieldKey}`];
            }
          } else if (fieldKey !== undefined && fieldKey.indexOf('[-]') > -1) {
            /* 处理赋值字段中[-]拼接 */
            const key = fieldKey.split('[-]')[0];
            const key1 = fieldKey.split('[-]')[1];
            slaveValue[`${child}`] = `${v[`${key}`]}[-]${v[`${key1}`]}`;
          } else {
            slaveValue[`${child}`] = v[`${fieldKey}`];
          }
        }
        slaveValue.sId = v.sId;
        masterSlaveData4.push(slaveValue);
      }
    }
  }
  if (masterSlaveData4.length > 0) {
    return {
      masterData, masterSlaveData, masterSlaveData2, masterSlaveData3, masterSlaveData4,
    };
  } else if (masterSlaveData3.length > 0) {
    return {
      masterData, masterSlaveData, masterSlaveData2, masterSlaveData3,
    };
  } else if (masterSlaveData2.length > 0) {
    return { masterData, masterSlaveData, masterSlaveData2 };
  } else {
    return { masterData, masterSlaveData };
  }
}

/* 预留字段布局显示 */
export function Reserve(mainConfig, childProps) {
  const reserveData = [];
  mainConfig.forEach((child) => {
    const { sName } = child;
    if (sName.indexOf('Reserve') > 0) {
      reserveData.push({
        sName: child.sName,
      });
    }
  });
  const reserveContent = [];
  reserveData.forEach((child) => {
    const { sName } = child;
    const showConfig = mainConfig.filter(item => (item.sName === sName))[0];
    const reserveProps = { showConfig, ...childProps };
    reserveContent.push(
      <Col
        key={showConfig.sId}
        span={6}
        order={showConfig.iOrder > 24 ? 24 : showConfig.iOrder}
        style={{ display: showConfig.bVisible ? 'block' : 'none' }}
      >
        <ShowType sName={child.sName} {...reserveProps} />
      </Col>);
  });
  return reserveContent;
}

/* 数组排序函数根据iOrder进行排序 */
export function iOrderSort(a, b) {
  return a.iOrder - b.iOrder;
}
/* 获取按钮显示名
*  配置
*  后台配置
*  常量
*  默认值
* */
export function getBtnShowName(config, gdsconfigformslave) {
  const returnConfigs = [];
  for (const child of config) {
    let returnConfig = {};
    [returnConfig] = gdsconfigformslave.filter(item => item.sControlName === child.sName);
    if (returnConfig !== undefined && Object.keys(returnConfig).length > 0) {
      child.showName = returnConfig.showName;
      returnConfigs.push(child);
    } else {
      returnConfigs.push(child);
    }
  }
  return returnConfigs;
}

// /* 从数组中返回一个对象
// *
// * */
export function getControlConfig(sControlName, arrs) {
  const ret = arrs.find((value) => {
    return value.sControlName === sControlName;
  });
  return ret;
}

/**
 * 替换下拉主表数据
 * 下拉赋值字段存在取主表数据
 * 需替换数据集
 * 主表数据集
 */
export function replaceSelectMasterData(data, masterData) {
  if (masterData !== undefined && Object.keys(masterData).length > 0) {
    for (const child of data) {
      for (const item of Object.keys(child)) {
        if (typeof (child[item]) === 'string' && child[item].indexOf('#') > -1) {
          child[item] = masterData[item];
        }
      }
    }
  }
  return data;
}


export function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

export function isNotEmpty(data) {
  return data !== undefined && data.trim() !== '';
}

export function getDateFormat(systemData) {
  const sCbxDateTime = systemData.filter(item => (item.sName === 'CbxDateTime'));
  const [{ sValue }] = sCbxDateTime;
  let dateFormat = 'YYYY-MM-DD';
  if (sValue !== undefined) {
    if (sValue === '1') {
      dateFormat = 'YYYY-MM-DD HH:mm:ss';
    } else if (sValue === '3') {
      dateFormat = 'HH:mm:ss';
    }
  }
  return dateFormat;
}

/** 结算日期计算 */
export function getGeyPayDate(data, mainData, dateFormat, sType, value) {
  const sGetPayTypeData = data.filter(item => item.key === 'sGetPayType');
  const { tCreateDate } = mainData;
  const tDeliverDate = sType === 'deliver' ? value.tDeliverDate : mainData.tDeliverDate;
  const tCreateDateValue = tCreateDate !== undefined && tCreateDate !== null ? moment(tCreateDate).format(dateFormat) : moment().format(dateFormat);
  const tDeliverDateValue = tDeliverDate !== undefined && tDeliverDate !== null ? moment(tDeliverDate).format(dateFormat) : tCreateDateValue;
  if (sGetPayTypeData.length > 0) {
    const sGetPayType = sGetPayTypeData[0].value;
    if (sGetPayType === 'now') {
      mainData.tGetPayDate = tCreateDateValue;
    } else if (sGetPayType === 'month') {
      const iMonth = data.filter(item => item.key === 'iMonth')[0].value;
      const iDay = data.filter(item => item.key === 'iDay')[0].value;
      const tCreateValue = moment(tCreateDate).set('date', iDay);
      if (iMonth === '0') {
        const tCurrentDay = moment(tCreateDate).get('date');
        if (tCurrentDay < iDay) {
          mainData.tGetPayDate = moment(tCreateValue).format(dateFormat);
        } else {
          mainData.tGetPayDate = moment(tCreateValue).add(1, 'months').format(dateFormat);
        }
      } else {
        mainData.tGetPayDate = moment(tCreateValue).add(iMonth, 'months').format(dateFormat);
      }
    } else if (sGetPayType === 'deliver') {
      const iAfterDeliverDay = data.filter(item => item.key === 'iAfterDeliverDay')[0].value;
      mainData.tGetPayDate = moment(tDeliverDateValue).add(iAfterDeliverDay, 'days').format(dateFormat);
    }
  }
  return mainData;
}

/**   设置主表渲染(全是true或false)   */
export function setMasterRenderTrueOrFalse(masterShowConfig, value) {
  /*   返回值声明   */
  const isMasterRender = {};
  /*   主表配置有值才进行容器赋值,否则就返回空对象了   */
  if (commonUtils.isNotEmptyArr(masterShowConfig)) {
    /*   遍历主表配置结构:[{},{},{}...]   */
    for (const each of masterShowConfig) {
      /*   sName为key,初始值都是false   */
      isMasterRender[each.sName] = value;
    }
  }
  /*   返回值   */
  return isMasterRender;
}

/**   设置主表渲染容器值   */
export function setMasterRenderContainerValue(isMasterRender, masterShowRender) {
  /*   如果没设置渲染数组就直接返回都是false的渲染容器即可   */
  if (commonUtils.isNotEmptyArr(masterShowRender)) {
    /*   渲染数组有值的话就遍历并把在渲染容器中对应的sName都变成true   */
    for (const sName of masterShowRender) {
      isMasterRender[sName] = true;
    }
  }
  /*   返回值   */
  return isMasterRender;
}

/**   设置从表渲染(全是true或false)   */
export function setSlaveRenderTrueOrFalse(slaveShowConfig, slaveData) {
  /*   返回值声明,数据结构{sSlaveId1:{sProductId:true,sProductQty:false},sSlaveId2:{sProductId:true,sProductQty:false},sSlaveId3:{sProductId:true,sProductQty:false}...}   */
  const isSlaveRender = {};
  /*   从表配置和从表数据都必须有值才进行容器赋值,否则就返回空对象了   */
  if (commonUtils.isNotEmptyArr(slaveShowConfig) && commonUtils.isNotEmptyArr(slaveData)) {
    /*   遍历从表数据:[{},{},{}...]   */
    for (const eachSlaveData of slaveData) {
      /*   声明容器内部对象   */
      const isSlaveRenderEach = {};
      /*   遍历从表配置:[{},{},{}...]   */
      for (const eachSlaveShowConfig of slaveShowConfig) {
        /*   给每一个从表配置内部对象赋默认值为false   */
        isSlaveRenderEach[eachSlaveShowConfig.sName] = false;
      }
      /*   给渲染容器对象的row赋值   */
      isSlaveRender[eachSlaveData.sId] = isSlaveRenderEach;
    }
  }
  /*   返回值   */
  return isSlaveRender;
}

/**   设置从表渲染容器值   */
export function setSlaveRenderContainerValue(isSlaveRender, slaveShowRender) {
  /*   如果没设置渲染对象就直接返回都是false的渲染容器即可   */
  if (commonUtils.isNotEmptyObject(slaveShowRender)) {
    /*   渲染对象有值的话就遍历并把在渲染容器中对应的sName都变成true   */
    for (const each of slaveShowRender) {
      /*   获取渲染对象中的从表编号   */
      const sSlaveId = Object.keys(each)[0];
      /*   获取渲染对象中的从表数据   */
      const valueArr = slaveShowRender[sSlaveId];
      /*   获取渲染容器中对应的从表row   */
      const isSlaveRenderEach = isSlaveRender[sSlaveId];
      /*   如果如安然对象的从表数据有值,则把这个值付给渲染对象   */
      if (commonUtils.isNotEmptyArr(valueArr)) {
        /*   循环赋值   */
        for (const key of valueArr) {
          isSlaveRenderEach[key] = true;
        }
      }
    }
  }
  /*   返回值   */
  return isSlaveRender;
}

/**   添加合计列渲染   */
export function addDataTotalRender(slaveShowConfig, slaveData, isSlaveRender) {
  /*   合计容器不为空并且从表数据也不为空才可以添加   */
  if (commonUtils.isNotEmptyArr(slaveData) && commonUtils.isNotEmptyObject(isSlaveRender)) {
    /*   过滤从表中的合计项   */
    const slaveDataDataTotal = slaveData.filter(item => item.dataTotal);
    /*   如果从表中存在合计项   */
    if (commonUtils.isNotEmptyArr(slaveDataDataTotal)) {
      /*   获取从表编号   */
      const slaveId = slaveDataDataTotal[0].sId;
      /*   获取合计列   */
      const dataTotal = getDataTotalColumn(slaveShowConfig);
      /*   如果存在合计列,那就给渲染容器添加合计项渲染   */
      if (commonUtils.isNotEmptyObject(dataTotal)) {
        /*   合计项渲染数组   */
        const dataTotalArr = [];
        /*   遍历合计项给合计项的渲染数组赋值   */
        for (const key of Object.keys(dataTotal)) {
          dataTotalArr.push(key);
        }
        /*   给渲染容器添加合计项渲染   */
        isSlaveRender[slaveId] = dataTotalArr;
      }
    }
  }
  /*   返回值   */
  return isSlaveRender;
}
// -------------------------从表单据渲染end  ------------------------- //
// ----------------------------单据渲染end  ---------------------------- //

// ----------------------------合计列start---------------------------- //
/**   获取合计列数据   */
export function getDataTotalColumn(slaveShowConfig) {
  /*   返回值   */
  const totalData = {};
  /*   如果存在从表配置   */
  if (commonUtils.isNotEmptyArr(slaveShowConfig)) {
    /*   遍历从表配置   */
    for (const each of slaveShowConfig) {
      /*   将需要合计的sName存入合计列对象中   */
      if (each.bSum) {
        totalData[each.sName] = 0;
      }
    }
  }
  /*   返回值   */
  return totalData;
}
// ----------------------------合计列end  ---------------------------- //


// ----------------------新记录-------------------------------------------------------------------------
/** 根据表配置获取表头 */
export function getHeaderConfig(config) {
  const tableConfig = config.gdsconfigformslave;
  const column = [];
  for (const child of tableConfig) {
    if (child.sName !== '' && child.bVisible && child.showName !== '') {
      column.push({
        title: child.showName,
        dataIndex: child.sName,
        width: child.iFitWidth,
        bFind: child.bFind,
      });
    }
  }
  return column;
}


/** 把主表字段绑定要相应的控件上 */
export function mapPropsToFields(masterData, form) {
  const result = {};
  if (commonUtils.isNotEmptyObject(masterData)) {
    Object.keys(masterData).forEach((child) => {
      const firstKey = child.substring(0, 1);
      if (firstKey === 'c') {
        const value = commonUtils.isUndefined(masterData[child]) ? [] : typeof masterData[child] === 'string' ?
          masterData[child].split(',') : commonUtils.convertUndefinedToEmptyArr(masterData[child]);
        result[child] = form.createFormField({ value });
      } else if (firstKey !== 't') {
        result[child] = form.createFormField({ value: masterData[child] });
      } else {
        const dateFormat = 'YYYY-MM-DD HH:mm:ss';
        /* 日期控件显示格式 */
        const value = masterData[child] !== null ? moment(masterData[child], dateFormat) : null;
        result[child] = form.createFormField({ value });
      }
    });
  }
  return result;
}

/** 获取数据默认值  取消其他表值数据时  sTaxId:master.sTaxId */
export function getDefaultData(config, allTableData) {
  const returnData = {};
  config.gdsconfigformslave.forEach((childConfig) => {
    const { sDefault, sName } = childConfig;
    if (sDefault !== '' && sDefault !== undefined && sDefault.indexOf(':') > -1 && sDefault.indexOf('.') > -1) {
      const oldFieldName = sDefault.split(':')[1];
      const tableName = oldFieldName.split('.')[0];
      const oldTableFieldName = oldFieldName.split('.')[1];
      if (!commonUtils.isEmptyObject(allTableData[tableName])) {
        returnData[sName] = allTableData[tableName][oldTableFieldName];
      }
    } else if (sDefault !== '' && sDefault !== undefined) {
      if (sName.substring(0, 1) === 'b') {
        returnData[sName] = commonUtils.converNumStrToBoolean(sDefault);
      } else {
        returnData[sName] = sDefault;
      }
    }
  });
  return returnData;
}

/** 获取数据默认值 */
export function getAssignFieldValue(sAssignField, srcData, allTableData) {
  const returnData = {};
  if (sAssignField !== '') {
    /* 赋值数组 */
    const sAssignFieldObj = sAssignField.split(',');
    if (commonUtils.isNotEmptyObject(srcData)) {
      sAssignFieldObj.forEach((item) => {
        if (item.indexOf(':') > -1) {
          const newFieldName = item.split(':')[0];
          const oldFieldName = item.split(':')[1];
          if (oldFieldName.includes('+') || oldFieldName.includes('-') || oldFieldName.includes('*') ||
            oldFieldName.includes('/') || oldFieldName.includes('(') || oldFieldName.includes(')')) {
            let sFormula = oldFieldName;
            let sFormulaSplit = oldFieldName;
            sFormulaSplit = sFormulaSplit.replace('+', '$');
            sFormulaSplit = sFormulaSplit.replace('-', '$');
            sFormulaSplit = sFormulaSplit.replace('*', '$');
            sFormulaSplit = sFormulaSplit.replace('/', '$');
            sFormulaSplit = sFormulaSplit.replace('(', '$');
            sFormulaSplit = sFormulaSplit.replace(')', '$');
            sFormulaSplit.split('$').forEach((oldFieldNameItem) => {
              const oldFieldItem = oldFieldNameItem.trim();
              if (oldFieldItem.indexOf('.') > -1) {
                const tableName = oldFieldItem.split('.')[0].trim();
                const oldTableFieldName = oldFieldItem.split('.')[1].trim();
                if (!commonUtils.isEmptyObject(allTableData[tableName])) {
                  sFormula = sFormula.replace(oldTableFieldName, allTableData[tableName][oldTableFieldName]);
                }
              } else {
                const oldTableFieldName = oldFieldItem.trim();
                sFormula = sFormula.replace(oldTableFieldName, srcData[oldTableFieldName]);
              }
            });
            returnData[newFieldName] = eval(sFormula);
          } else if (oldFieldName.indexOf('.') > -1) {
            const tableName = oldFieldName.split('.')[0].trim();
            const oldTableFieldName = oldFieldName.split('.')[1].trim();
            if (!commonUtils.isEmptyObject(allTableData[tableName])) {
              returnData[newFieldName] = allTableData[tableName][oldTableFieldName];
            }
          } else {
            returnData[newFieldName] = srcData[oldFieldName];
          }
        }
      });
    }
  }
  return returnData;
}


/** 获取常量数据 */
export function showMessage(gdsformconst, sName) {
  const iIndex = gdsformconst.findIndex(item => item.sName === sName);
  if (iIndex > -1) {
    return gdsformconst[iIndex].showName;
  }
  return '';
}

/** 获取配置showName */
export function getConfigShowName(config, sName) {
  const iIndex = config.gdsconfigformslave.findIndex(item => item.sName === sName);
  if (iIndex > -1) {
    return config.gdsconfigformslave[iIndex].showName;
  }
  return '';
}

/* 根据配置取到 快捷过滤的字段 */
export function getConfigFieldNameData(config, sFieldName) {
  return config.gdsconfigformslave.filter(item => (item.sName !== '' && item.bVisible && item[sFieldName] && item.showName !== ''));
}

/** 获取系统设定的值 */
export function getSystemSettingValue(sName) {
  const systemSettings = commonUtils.getSystemSettings();
  const iIndex = systemSettings.findIndex(item => item.sName === sName);
  if (iIndex > -1) {
    return systemSettings[iIndex].sValue;
  }
  return '';
}

export function getImitateGdsconfigTypes(showName, sControlName, sName, bNotEmpty, sDropDownType, showDropDown, upload) {
  const returnTypes = {
    bCanInput: false,
    bFilter: false,
    bFind: false,
    bFirstEmpty: false,
    bIsOther: false,
    bNewRecord: false,
    bNotEmpty: false,
    bNotRepeat: false,
    bReadonly: false,
    bSum: false,
    bTree: false,
    bVisible: true,
    iColValue: 1,
    iFitWidth: 120,
    iIncrement: 25502,
    iOrder: 0,
    iTag: 0,
    iVisCount: 0,
    sActiveKey: '',
    sAssignField: '',
    sBig5: '',
    sBig5DropDown: '',
    sBrandsId: '1111111111',
    sChinese: showName,
    sChineseDropDown: '',
    sControlName,
    sDefault: '',
    sDropDownType,
    sEnglish: '',
    sEnglishDropDown: '',
    sFilterSql: '',
    sId: commonUtils.createSid(),
    sKeyUpFilter: '',
    sMaxValue: '',
    sMinValue: '',
    sName: sName === undefined ? '' : sName,
    sNotRepeatColumn: '',
    sParentId: '19211681019715560848632490',
    sRelation: '',
    sSelectRelation: '',
    sSqlCondition: '',
    sSubsidiaryId: '1111111111',
    showDropDown,
    showName,
    needUpload: upload, // 选择的同时并上传 默认不上传 false， true表示上传
  };
  return returnTypes;
}

export function getImitateTypes(gdsconfigformslave) {
  const returnTypes = {
    gdsconfigformslave,
  };
  return returnTypes;
}

